﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class AddUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] != null)
        {
           if (!IsPostBack)
            {
           
                BindDetails();
            }
            
        }
        else
        {
            Response.Redirect(GetUrlPath.GetUrl + "/Login.aspx");
        }

    }
    //读取角色类型，读取部门名称
    protected void BindDetails()
    {
           //读取角色类型，绑定到ddlRole下拉列表
            string strSelectRole = "select * from ROLE";
            DBOperation db = new DBOperation();
            SqlDataReader sdr = db.GetDataReader(strSelectRole);
            ddlRole.DataSource = sdr;
            ddlRole.DataTextField = "ROLENAME";
            ddlRole.DataValueField = "ROLEID";
            ddlRole.DataBind();

            sdr.Close();
            //读取部门名称，绑定到ddlDep下拉列表

            string strSelectDep = "select * from DEPARTMENT";
            sdr = db.GetDataReader(strSelectDep);
            ddlDep.DataSource = sdr;
            ddlDep.DataTextField = "DEPNAME";
            ddlDep.DataValueField = "DEPID";
            ddlDep.DataBind();
            //绑定数据后及时关闭连接
            sdr.Close();
    }
    protected void btnTijiao_Click(object sender, EventArgs e)
    {
        DataRow curentUser = Users.searchUser(txtName.Text.Trim());
        //如果不存在这个用户名，就建立一个新的user对象（即添加一个用户）
        if (curentUser == null)
        {


            string name = txtName.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            string trueName = txtTrueName.Text.Trim();
            int  depID =Convert.ToInt32( ddlDep.SelectedValue);
            int roleID = Convert.ToInt32(ddlRole.SelectedValue);
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();

            Users newUser = new Users(name, pwd, trueName, depID, roleID, phone, email);

            if (newUser.AddUser())

                Response.Write("<script language='javascript'>alert('添加成功！')</script>");

        }
        else
        {
            Response.Write("<script language='javascript'>alert('用户名已经存在，请使用另一个用户名！')</script>");
        }

    }
    //取消注册用户
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtEmail.Text = "";
        txtName.Text = "";
        txtPhone.Text = "";
        txtPwd.Text = "";
        txtTrueName.Text = "";
    }
}
